package com.tiger.project.service.impl;

import java.util.List;

import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.AssetReceipt;
import com.tiger.project.domain.AssetReceiptAccount;
import com.tiger.project.mapper.AssetReceiptAccountEnclosureMapper;
import com.tiger.project.mapper.AssetReceiptAccountMapper;
import com.tiger.project.mapper.AssetReceiptMapper;
import com.tiger.project.service.IAssetReceiptAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【租金到账】Service业务层处理
 *
 * @author standard
 * @date 2022-11-22
 */
@Service
public class AssetReceiptAccountServiceImpl implements IAssetReceiptAccountService {

    @Autowired
    private AssetReceiptAccountMapper assetReceiptAccountMapper;
    @Autowired
    private AssetReceiptMapper assetReceiptMapper;
    @Autowired
    private AssetReceiptAccountEnclosureMapper enclosureMapper;

    /**
     * 查询【租金到账】
     *
     * @param id 【租金到账】主键
     * @return 【租金到账】
     */
    @Override
    public AssetReceiptAccount selectAssetReceiptAccountById(Long id) {
        return assetReceiptAccountMapper.selectAssetReceiptAccountById(id);
    }

    @Override
    public AssetReceipt selectAssetReceipt(Long id) {
        AssetReceipt assetReceipt = assetReceiptMapper.selectAssetReceiptById(id);
        AssetReceiptAccount account = new AssetReceiptAccount();
        account.setAssetReceiptId(id);
        List<AssetReceiptAccount> accounts = assetReceiptAccountMapper.selectAssetReceiptAccountList(account);
        if (accounts != null && accounts.size()>0){
            assetReceipt.getAssetReceiptAccountList().addAll(accounts);
        }
        return assetReceipt;
    }

    /**
     * 查询【租金到账】列表
     *
     * @param assetReceiptAccount 【租金到账】
     * @return 【租金到账】
     */
    @Override
    public List<AssetReceiptAccount> selectAssetReceiptAccountList(AssetReceiptAccount assetReceiptAccount) {
        return assetReceiptAccountMapper.selectAssetReceiptAccountList(assetReceiptAccount);
    }

    /**
     * 新增【租金到账】
     *
     * @param assetReceiptAccount 【租金到账】
     * @return 结果
     */
    @Override
    public int insertAssetReceiptAccount(AssetReceiptAccount assetReceiptAccount) {
        int i = assetReceiptAccountMapper.insertAssetReceiptAccount(assetReceiptAccount);
        change(assetReceiptAccount);
        return i;
    }

    /**
     * 修改【租金到账】
     *
     * @param assetReceiptAccount 【租金到账】
     * @return 结果
     */
    @Override
    public int updateAssetReceiptAccount(AssetReceiptAccount assetReceiptAccount) {
        change(assetReceiptAccount);
        return assetReceiptAccountMapper.updateAssetReceiptAccount(assetReceiptAccount);
    }

    /**
     * 批量删除【租金到账】
     *
     * @param ids 需要删除的【租金到账】主键
     * @return 结果
     */
    @Override
    public int deleteAssetReceiptAccountByIds(Long[] ids) {
        return assetReceiptAccountMapper.deleteAssetReceiptAccountByIds(ids);
    }

    /**
     * 删除【租金到账】信息
     *
     * @param id 【租金到账】主键
     * @return 结果
     */
    @Override
    public int deleteAssetReceiptAccountById(Long id) {
        return assetReceiptAccountMapper.deleteAssetReceiptAccountById(id);
    }


    private void change(AssetReceiptAccount assetReceiptAccount){
        enclosureMapper.delete(assetReceiptAccount.getId());
        if (StringUtils.isNotEmpty(assetReceiptAccount.getAccountEnclosureList())){
            assetReceiptAccount.getAccountEnclosureList().forEach(info ->{
                info.setCreatedAt(DateUtils.getNowDate());
                info.setAssetReceiptAccountId(assetReceiptAccount.getId());
                enclosureMapper.insertAssetReceiptAccountEnclosure(info);
            });
        }
    }

}
